/**
 * 选人
 */
function ActorModule(ui, engine) {
	this.base = Module;
	this.base(ui, engine);
}
ActorModule.prototype = new Module();

ActorModule.prototype.alert = function() {

};

ActorModule.prototype.openSelector = function(data) {
	console.log(data);
	this.actorData = data;

	var actorModule = this;

	var actPanel = function(title) {
		var html = '<div class="panel panel-default"><div class="panel-heading">' + title + '</div><div class="panel-body"></div></div>';
		return $(html);
	};

	var option = function(type, val, name, checked, index) {
		var html = '<div class="radio"><label><input type="' + type + '" name="actor' + index + '" value="' + val + '" ' + checked + '>' + name + '</label></div>';
		return $(html);
	};

	var wrap = $("<div></div>");

	for (var i = 0; i < actorModule.actorData.settings.length; i++) {
		var actors = actorModule.actorData.settings[i].actors;
		var activityName = actorModule.actorData.settings[i].activityName;
		var actorNUM = actorModule.actorData.settings[i].actorNUM;
		var inputType = "radio";
		if (actorNUM > 1)
			inputType = "checkbox";

		var p = actPanel(activityName);
		for (var j = 0; j < actors.length; j++) {
			var name = actors[j].name;
			var val = actors[j].id;
			var o = option(inputType, val, name, "", i);
			var input = o.find("input");
			input.attr("i", i);
			input.attr("j", j);
			input.click(function() {
				var ii = $(this).attr("i");
				var jj = $(this).attr("j");

				if ($(this).prop("checked")) {
					actorModule.actorData.settings[ii].actors[jj].uiChecked = true;
					if ($(this).attr("type") == "radio") {
						for (var n = 0; n < actorModule.actorData.settings[ii].actors.length; n++) {
							if (n != jj)
								actorModule.actorData.settings[ii].actors[n].uiChecked = false;
						}
					}
				} else {
					actorModule.actorData.settings[ii].actors[jj].uiChecked = false;
				}
			});
			p.find(".panel-body").append(o);
		}
		p.find(".panel-body").append(
				'<div>提示：选择<span class="badge" style="margin:0 5px;">' + actorNUM + '</span>人</div>');
		wrap.append(p);
	}

	dialog.modal1({
	title : "选择处理人",
	sourceType : "local",// local 表示内容来源本地, remote 表示内容从网络请求获取
	htmlContent : wrap,// 若sourceType=local时，htmlContent不能为空
	btnFunction : function() {
		return actorModule.submit();
	},
	closeModalOnBtnFunctionTrigger : false
	});
};

ActorModule.prototype.submit = function() {
	var actorModule = this;
	var todoId = this.engine.params.todoId;
	var aStr = "";
	for (var i = 0; i < this.actorData.settings.length; i++) {
		var acName = this.actorData.settings[i].activityName;
		var acNUM = this.actorData.settings[i].actorNUM;
		aStr += acName + ":";
		var realNUM = 0;
		for (var j = 0; j < this.actorData.settings[i].actors.length; j++) {
			if (this.actorData.settings[i].actors[j].uiChecked) {
				aStr += this.actorData.settings[i].actors[j].id + ",";
				++realNUM;
			}
		}

		if (acNUM != realNUM) {
			dialog.alert(acName + "  环节需要" + acNUM + "人处理，当前选了" + realNUM + "人！");
			return false;
		}

		aStr += ";";
	}

	$.ajax({
	url : ctxPath + '/jbpmService/engine/submitActor',
	type : 'post',
	dataType : 'text',
	data : 'actors=' + aStr + '&todoId=' + todoId,
	success : function(data) {
		var obj = Util.strToObj(data);
		if (obj) {
			if (obj.type == 'end') {
				actorModule.engine.complete();
			} else if (obj.type == 'error') {

			}
			dialog.alert(obj.message);
		} else
			dialog.alert("提交异常！");
	}
	});

	return true;
};